package com.mago.tis.utility;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

import com.itextpdf.text.Anchor;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class PdfGenerator {
	private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,
			Font.BOLD);
	private static Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12,
			Font.NORMAL, BaseColor.RED);
	private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16,
			Font.BOLD);
	private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12,
			Font.BOLD);
	private static Font tableHeaderFont = new Font(Font.FontFamily.TIMES_ROMAN,
			9, Font.BOLD);
	private static Font tableCellFont = new Font(Font.FontFamily.TIMES_ROMAN,
			8, Font.NORMAL);

	public static void main(String[] args) {
		try {
			OutputStream file = new FileOutputStream(new File("D:\\Test.pdf"));

			Document document = new Document(PageSize.A4);
			PdfWriter.getInstance(document, file);

			document.open();
			// document.add(new Paragraph("Hello World, iText"));
			// document.add(new Paragraph(new Date().toString()));
			addContent(document);

			document.close();
			file.close();

		} catch (Exception e) {

			e.printStackTrace();
		}
	}

	private static void createTable(Section subCatPart, List dataList)
			throws BadElementException, DocumentException {
		System.out.println("Table being created");
		if (dataList != null && dataList.isEmpty() != true) {
			PdfPTable table = new PdfPTable(7);

			/**
			 * Table Header Section Start
			 */
			PdfPCell headerCell = new PdfPCell(new Phrase("S. No.",
					tableHeaderFont));
			headerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(headerCell);

			headerCell = new PdfPCell(new Phrase("Product Code",
					tableHeaderFont));
			headerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(headerCell);

			headerCell = new PdfPCell(new Phrase("Name", tableHeaderFont));
			headerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(headerCell);

			headerCell = new PdfPCell(
					new Phrase("Description", tableHeaderFont));
			headerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(headerCell);

			headerCell = new PdfPCell(new Phrase("Quantity", tableHeaderFont));
			headerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(headerCell);

			headerCell = new PdfPCell(new Phrase("Coupon", tableHeaderFont));
			headerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(headerCell);

			headerCell = new PdfPCell(new Phrase("Amount", tableHeaderFont));
			headerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(headerCell);
			table.setHeaderRows(1);
			/**
			 * Table Header Section Close
			 */

			/**
			 * Table Data Section Start
			 */
			PdfPCell dataCell = null;
			for (int counter = 0; counter < dataList.size(); counter++) {
				Object[] data = (Object[]) dataList.get(counter);
				dataCell = new PdfPCell(new Phrase("" + (counter + 1),
						tableCellFont));
				dataCell.setHorizontalAlignment(Element.ALIGN_CENTER);
				table.addCell(dataCell);

				if (data[0] != null && !data[0].equals(""))
					dataCell = new PdfPCell(new Phrase("" + data[0],
							tableCellFont));
				else
					dataCell = new PdfPCell(new Phrase("-", tableCellFont));
				dataCell.setHorizontalAlignment(Element.ALIGN_CENTER);
				table.addCell(dataCell);

				if (data[1] != null && !data[1].equals(""))
					dataCell = new PdfPCell(new Phrase("" + data[1],
							tableCellFont));
				else
					dataCell = new PdfPCell(new Phrase("-", tableCellFont));
				dataCell.setHorizontalAlignment(Element.ALIGN_CENTER);
				table.addCell(dataCell);

				if (data[2] != null && !data[2].equals(""))
					dataCell = new PdfPCell(new Phrase("" + data[2],
							tableCellFont));
				else
					dataCell = new PdfPCell(new Phrase("-", tableCellFont));
				dataCell.setHorizontalAlignment(Element.ALIGN_CENTER);
				table.addCell(dataCell);

				if (data[3] != null && !data[3].equals(""))
					dataCell = new PdfPCell(new Phrase("" + data[3],
							tableCellFont));
				else
					dataCell = new PdfPCell(new Phrase("-", tableCellFont));
				dataCell.setHorizontalAlignment(Element.ALIGN_CENTER);
				table.addCell(dataCell);

				if (data[4] != null && !data[4].equals(""))
					dataCell = new PdfPCell(new Phrase("" + data[4],
							tableCellFont));
				else
					dataCell = new PdfPCell(new Phrase("-", tableCellFont));
				dataCell.setHorizontalAlignment(Element.ALIGN_CENTER);
				table.addCell(dataCell);

				if (data[5] != null && !data[5].equals(""))
					dataCell = new PdfPCell(new Phrase("" + data[5],
							tableCellFont));
				else
					dataCell = new PdfPCell(new Phrase("-", tableCellFont));
				dataCell.setHorizontalAlignment(Element.ALIGN_CENTER);
				table.addCell(dataCell);
			}
			/**
			 * Table Data Section End
			 */

			/**
			 * Other Information Section Start
			 */
			dataCell = new PdfPCell(new Phrase("Total Amount", tableCellFont));
			dataCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
			dataCell.setColspan(6);
			table.addCell(dataCell);
			// TO DO
			dataCell = new PdfPCell(new Phrase("5300", tableCellFont));
			dataCell.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(dataCell);

			dataCell = new PdfPCell(
					new Phrase("Courier Charges", tableCellFont));
			dataCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
			dataCell.setColspan(6);
			table.addCell(dataCell);
			dataCell = new PdfPCell(new Phrase("500", tableCellFont));
			dataCell.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(dataCell);

			dataCell = new PdfPCell(new Phrase("Total Amount", tableCellFont));
			dataCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
			dataCell.setColspan(6);
			table.addCell(dataCell);
			dataCell = new PdfPCell(new Phrase("89", tableCellFont));
			dataCell.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(dataCell);

			dataCell = new PdfPCell(new Phrase("Discount", tableCellFont));
			dataCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
			dataCell.setColspan(6);
			table.addCell(dataCell);
			dataCell = new PdfPCell(new Phrase("5", tableCellFont));
			dataCell.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(dataCell);

			dataCell = new PdfPCell(new Phrase("Vat 1%", tableCellFont));
			dataCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
			dataCell.setColspan(6);
			table.addCell(dataCell);
			dataCell = new PdfPCell(new Phrase("4", tableCellFont));
			dataCell.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(dataCell);

			dataCell = new PdfPCell(new Phrase("Grand Total", tableCellFont));
			dataCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
			dataCell.setColspan(6);
			table.addCell(dataCell);
			dataCell = new PdfPCell(new Phrase("5000", tableCellFont));
			dataCell.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(dataCell);
			/**
			 * Other Information Section End
			 */

			subCatPart.add(table);
		}
		System.out.println("Table created");

	}

	private static void addContent(Document document) throws DocumentException {
		System.out.println("Content being written");
		// Anchor anchor = new Anchor("First Chapter", catFont);
		Anchor anchor = new Anchor("First Chapter");
		anchor.setName("First Chapter");

		// Second parameter is the number of the chapter
		Chapter catPart = new Chapter(new Paragraph(anchor), 1);

		Paragraph subPara = new Paragraph("Subcategory 1");
		Section subCatPart = catPart.addSection(subPara);
		subCatPart.add(new Paragraph("Hello"));

		subPara = new Paragraph("Subcategory 2", subFont);
		subCatPart = catPart.addSection(subPara);
		subCatPart.add(new Paragraph("Paragraph 1"));
		subCatPart.add(new Paragraph("Paragraph 2"));
		subCatPart.add(new Paragraph("Paragraph 3"));

		// add a list
		// createList(subCatPart);
		Paragraph paragraph = new Paragraph();
		addEmptyLine(paragraph, 5);
		subCatPart.add(paragraph);

		// add a table
		Object[] dataObject = null;
		List dataList = new ArrayList();
		dataObject = new Object[6];
		dataObject[0] = "PROD123";
		dataObject[1] = "Gold Necklace";
		dataObject[2] = "Golden Twinkling Necklace with Gem Stone";
		dataObject[3] = "25";
		dataObject[4] = "COU101";
		dataObject[5] = "3000";
		dataList.add(dataObject);
		dataObject = new Object[6];
		dataObject[0] = "PROD457";
		dataObject[1] = "BRONZE RING";
		dataObject[2] = "Bronze Color ring with golden polish";
		dataObject[3] = "5";
		dataObject[4] = "";
		dataObject[5] = "1500";
		dataList.add(dataObject);
		dataObject = new Object[6];
		dataObject[0] = "PROD573";
		dataObject[1] = "Diamond Pendant";
		dataObject[2] = "Diamond Pendant";
		dataObject[3] = "25";
		dataObject[4] = "COU101";
		dataObject[5] = "800";
		dataList.add(dataObject);
		createTable(subCatPart, dataList);

		// now add all this to the document
		document.add(catPart);

		// Next section
		// anchor = new Anchor("Second Chapter", catFont);
		// anchor.setName("Second Chapter");

		// Second parameter is the number of the chapter
		// catPart = new Chapter(new Paragraph(anchor), 1);

		// subPara = new Paragraph("Subcategory", subFont);
		// subCatPart = catPart.addSection(subPara);
		// subCatPart.add(new Paragraph("This is a very important message"));

		// now add all this to the document
		// document.add(catPart);
		System.out.println("Content written");
	}

	private static void addEmptyLine(Paragraph paragraph, int number) {
		for (int i = 0; i < number; i++) {
			paragraph.add(new Paragraph(" "));
		}
	}
}
